<?php
require_once('exceptions/configurationexception.class.php');

/**
 * <h2>Configuration file reader</h2>.
 * <p>multiple module are configured through the system.ini file</p>
 * <h3>i18n</h3>
 * <dl>
 * <dt>default</dt>
 * <dd>Default international language for the application. mainly <code>en-EN</code></dd>
 * </dl>
 * <h3>cache</h3>
 * <dl>
 * <dt>activate</dt>
 * <dd>Flag indicating if cache engine is activated or not. possible values : on/off</dd>
 * </dl>
 * <h3>html</h3>
 * <dl>
 * <dt>rewrite</dt>
 * <dd>flag to inform framework if http server rewrite engine is activated for this site. possible values: on/off</dd>
 * <dt>accesskey</dt>
 * <dd>template to display accesskey into links. default value is <code><u>{0}</u></code> where <code>{0}</code> is a place holder for the access key.</dd>
 * </dl>
 * <h3>jslib</h3>
 * <dl>
 * <dt>js.n</dt>
 * <dd>add la js librarry to the framework. this js library will be loaded on each generated page. <code>n</code> is a number setting an order of import.</dd>
 * </dl>
 * <h3>csslib</h3>
 * <dl>
 * <dt>{name}</dt>
 * <dd>a free name to set like <code>mycss=[media]:[filename]</dd>. where <code>media</code> is a css media like <code>screen</code>, <code>print</code>, or <code>handled</code>. the <code>filename</code> is a relative path from <code>{root}/</code></dd>
 * </dl>
 * <h3>debug</h3>
 * <dl>
 * <dt>show</dt>
 * <dd>Activate Debug window capabilities</dd>  
 * <dt>level</dt>
 * <dd>Set the trace level to be produced into the file and the debug window. values are:
 * <ul>
 * <li>debug</li>
 * <li>info</li>
 * <li>warn</li>
 * <li>error</li>
 * <li>fatal</li>
 * </ul>
 * </dd>  
 * <dt>path</dt>
 * <dd></dd>  
 * <dt>format</dt>
 * <dd></dd>  
 * </dl>
 * <h3>database</h3>
 * <dl>
 * <dt>type</dt>
 * <dd>Type of database implementation to use. actually only MySQL database are managed by the framework (value: mysql). A new database implementation must inherited from <code>Connection</code> class and implements the <code>IConnection</code> interface.</dd>  
 * <dt>servername</dt>
 * <dd>name of the server.</dd>  
 * <dt>portnumber</dt>
 * <dd>Port Number for the database serveice (for MySQL : 3306)</dd>  
 * <dt>username</dt>
 * <dd>Database connection user name. CAREFUL: for schema generation on database, user must have DBA access.</dd>  
 * <dt>password</dt>
 * <dd>Database conneciton password</dd>  
 * <dt>database</dt>
 * <dd>Name of the database on the server.</dd>  
 * </dl>
 * @author delormef
 * @version 1.0 - 12/12/2009
 */
class Config{
	/**
	 * Instance
	 * @var Config
	 */
	static private $_instance=null;
	/**
	 * List of the initialization file parameters.
	 * @var array
	 */
	private $parameters = array();

	/**
	 * default constructor
	 */
	function __construct(){
		$this->parameters=parse_ini_file(dirname($_SERVER['SCRIPT_FILENAME'])."/config/system.ini",true);
	}
	/**
	 * get a value from the configuration file (system.ini)
	 * @param $group group of keys.
	 * @param $key key of the value.
	 * @param $default a default value for the group/key.
	 * @return string containing the value.
	 */
	function get($group,$key,$default=""){
		if(isset($this->parameters[$group]) && isset($this->parameters[$group][$key])){
			return $this->parameters[$group][$key];
		}else if($default!=""){
			return $default;
		}else{
			return "system.ini key[$group,$key] not defined";
		}
	}

	/**
	 * get a group of value from the configuration file (system.ini) as array.
	 * @param $group group of keys.
	 * @return string containing the value.
	 */
	function getGroup($group){
		if(isset($this->parameters[$group])){
			return $this->parameters[$group];
		}else{
			throw new ConfigurationException("the requested group named '$group' does not exists. ");
		}
	}

	/**
	 * Test if a specific $group exists in configuration file.
	 * @param $group
	 * @return boolean
	 */
	function isExists($group=""){
		if($group!="" && isset($this->parameters[$group])){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * Test if a specific $group/$key couple exists ion configuration file.
	 * @param $group
	 * @param $key
	 * @return boolean
	 */
	function isKeyExists($group="",$key=""){
		if($group!="" && isset($this->parameters[$group]) &&
		$key!="" && isset($this->parameters[$key])){
			return true;
		}else{
			return false;
		}
	}


	/**
	 * return the static instance of the Configuration loader.
	 * create the instance if does not already exist.
	 */
	function getInstance(){
		if(!isset(self::$_instance)){
			self::$_instance = new Config();
		}
		return self::$_instance;
	}

}
?>
